package com.zzjc.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzjc.util.UUIDUtils;
import com.zzjc.web.config.GlobalConfig;
import com.zzjc.web.entity.SwimmingRoom;
import com.zzjc.web.entity.UploadFileRecord;
import com.zzjc.web.mapper.SwimmingRoomDao;
import com.zzjc.web.service.SwimmingRoomService;
import com.zzjc.web.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class SwimmingRoomServiceImpl implements SwimmingRoomService {

    @Autowired
    private SwimmingRoomDao swimmingRoomDao;
    @Autowired
    private GlobalConfig globalConfig;


    @Override
    public ApiResponse addRoom(SwimmingRoom room) {
        try {
            SwimmingRoom existingRoom = swimmingRoomDao.getRoomByName(room.getRoomName());
            if (existingRoom != null) {
                return ResponseUtil.error("游泳场所已存在,无需重复添加");
            }
            room.setCreateTime(DateUtils.getNowDate());
            int result = swimmingRoomDao.insertRoom(room);
            return ResponseUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("添加游泳场所失败请重试");
        }
    }

    @Override
    public ApiResponse<?> updateRoom(SwimmingRoom room) {
        if (StringKit.notBlank(room.getRoomId().toString())) {
            try {
                List<Integer> roomIdList = new ArrayList();
                roomIdList.add(room.getRoomId());
                List<SwimmingRoom> iRoom = swimmingRoomDao.getRoomById(roomIdList);
                if (iRoom.size() < 1) {
                    return ResponseUtil.error("游泳场所不存在");
                } else {
                    SwimmingRoom existingRoom = swimmingRoomDao.getRoomByName(room.getRoomName());
                    if (existingRoom != null) {
                        return ResponseUtil.error("该游泳场所名称已存在,无需重复添加");
                    }
                    room.setUpdateTime(DateUtils.getNowDate());
                    int result = swimmingRoomDao.updateRoom(room);
                    return ResponseUtil.success();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("修改游泳场所失败");
            }
        } else {
            return ResponseUtil.error("不存在游泳场所id");
        }
    }

    @Override
    public ApiResponse deleteRoom(List<Integer> roomList) {
        if (roomList.size() > 0) {
            try {
                List<SwimmingRoom> roomById = swimmingRoomDao.getRoomById(roomList);
                if (roomById.size() < 1) {
                    return ResponseUtil.error("游泳场所不存在");
                } else {
                    swimmingRoomDao.deleteRoom(roomList);
                    return ResponseUtil.success();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("删除游泳场所失败");
            }
        } else {
            return ResponseUtil.error("id不能为空");
        }
    }

    @Override
    public ApiResponse getAllRooms(SwimmingRoom room) {
        PageInfo<SwimmingRoom> pageInfo = null;
        try {
            int pageNum = 1;
            if (room.getPageNum() != null) {
                pageNum = room.getPageNum();
            }
            int pageSize = 1000;
            if (room.getPageSize() != null) {
                pageSize = room.getPageSize();
            }
            PageHelper.startPage(pageNum, pageSize);
            List<SwimmingRoom> roomList = swimmingRoomDao.findAll(room);
            pageInfo = new PageInfo<>(roomList);
            return ResponseUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("查询失败，请重试");
        }
    }

    @Override
    public ApiResponse doUpload(MultipartFile file, String roomId, HttpServletRequest request) {
        if (StringKit.notBlank(roomId)) {
            try {
                if (file.isEmpty()) {
                    return ResponseUtil.error("您的文件大小为0kb,已损坏暂不支持上传");
                }
                String uploadPath;
                if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
                    uploadPath = "D:\\swimmingFile";
                } else {
                    uploadPath = "/home/zzjc/swimmingFiles";
                }
                UploadUtil util = UploadUtil.build().uploadFile(file, uploadPath);
                UploadFileRecord record = new UploadFileRecord();
                String serviceUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/web/swimmingFile/";
                record.setFileId(UUIDUtils.getUUID());
                record.setRoomId(roomId);
                record.setFileRealName(util.getFileRealName());
                record.setFileName(util.getFileName());
                record.setFileExt(util.getExt());
                record.setLocalPath(util.getLocalFullPath());
                record.setFileUrl(serviceUrl + util.getUrlPath());
                swimmingRoomDao.insertRecord(record);
                return ResponseUtil.success();
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseUtil.error("上传失败，请重试");
            }
        } else {
            return ResponseUtil.error("不存在游泳场所id");
        }

    }

    @Override
    public ApiResponse getArea( SwimmingRoom room) {
        return (ResponseUtil.success(swimmingRoomDao.getArea(room)));
    }

} 